package com.smbms.tool;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.smbms.entity.User;

public class ExcelUtil {

	public String writerExcel(List<User> userList) throws IOException{
		//创建Excel工作空间
		HSSFWorkbook workbook=new HSSFWorkbook();

		//创建一页
		HSSFSheet sheet=workbook.createSheet();

		//获取第一行
		HSSFRow row =sheet.createRow(0);

		//获取第一格
		HSSFCell cell=row.createCell(0);

		cell.setCellValue("用户编码");
		//获取第二格
		cell=row.createCell(1);
		cell.setCellValue("用户名称");
		//获取第三格
		cell=row.createCell(2);
		cell.setCellValue("性别");
		//获取第4格
		cell=row.createCell(3);
		cell.setCellValue("年龄");
		//获取第5格
		sheet.setColumnWidth(4, 40*256);
		cell=row.createCell(4);
		cell.setCellValue("用户名称");
		//获取第6格
		cell=row.createCell(5);
		cell.setCellValue("用户名称");
         
		
		String path="D:/你好阿.xls";
		FileOutputStream out=new FileOutputStream(path);
		
		
		for (int i = 0; i < userList.size(); i++) {
			
			//写内空时从第二行开始 
			row=sheet.createRow(i+1);
			
			//获取该行的第一格
			cell=row.createCell(0);
			cell.setCellValue(userList.get(i).getUserCode());
			
			//获取该行的第二格
			cell=row.createCell(1);
			cell.setCellValue(userList.get(i).getUserName());
			
			//获取该行的第三格
			cell=row.createCell(2);
			if(userList.get(i).getGender()==1){
				cell.setCellValue("男");
			}else{
				cell.setCellValue("女");
			}
			
			//获取该行的第四格
			cell=row.createCell(3);
			cell.setCellValue(userList.get(i).getAge());
			
			//获取该行的第五格
			cell=row.createCell(4);
			cell.setCellValue(userList.get(i).getPhone());
			
			//获取该行的第六格
			cell=row.createCell(5);
			cell.setCellValue(userList.get(i).getUserRoleName());
			
		}
		
		workbook.write(out);
		
		out.close();
		
		return path;
	}

}
